package com.cy.基础.conllection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * List都是可重复,有序的集合,父类是Collection
 * public interface SequencedCollection<E> extends Collection<E>
 */
public class ListTest {

    /**
     * ArrayList
     */
    @Test
    public void test01() {
        //增
        List list = new ArrayList();
        list.add("中国");
        list.add("美国");
        list.add("日本");
        list.add("韩国");
        list.add("中国");
        //删
        list.remove(4);
        //改
        list.set(2, "小日本");
        //查
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    /**
     * LinkedList
     */
    @Test
    public void test02() {
        //增
        List list = new LinkedList();
        list.add("中国");
        list.add("美国");
        list.add("日本");
        list.add("韩国");
        list.add("中国");
        //删
        list.remove(4);
        //改
        list.set(2, "小日本");
        //查
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println(list.contains("中国"));
    }

    /**
     * ArrayList
     */
    @Test
    public void test03() {
        //增
        List list = new ArrayList();
        list.add(new Dog("欧欧", "雪纳瑞"));
        list.add(new Dog("丫丫", "拉布拉多"));
        list.add(new Dog("菲菲", "拉布拉多"));
        list.add(new Dog("关关", "雪纳瑞"));
        //查
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println(list.contains(new Dog("菲菲", "拉布拉多")));
        System.out.println("---------------------------");
        list.remove(new Dog("菲菲", "拉布拉多"));
        System.out.println(list.contains(new Dog("菲菲", "拉布拉多")));
    }

    /**
     * LinkedList
     */
    @Test
    public void test04() {
        //增
        LinkedList list = new LinkedList();
        list.add(new Dog("欧欧", "雪纳瑞"));
        list.add(new Dog("丫丫", "拉布拉多"));
        list.add(new Dog("菲菲", "拉布拉多"));
        list.add(new Dog("关关", "雪纳瑞"));
        list.addFirst(new Dog("菲菲2", "拉布拉多"));
        list.addLast(new Dog("关关2", "雪纳瑞"));
        Dog dog = (Dog) list.removeFirst();
        Dog dog2 = (Dog) list.removeLast();
        System.out.println(dog);
        System.out.println(dog2);
        System.out.println("---------------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

}
